/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mylhyl.circledialog.sample.provider;

import com.mylhyl.circledialog.sample.ResourceTable;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;

/**
 * 类功能描述(类上)
 *
 * @since 2021-07-30
 */
public class RvListProvider extends BaseItemProvider {
    private Context context;
    private List<String> data;
    private boolean isVertical;

    /**
     * RvListProvider
     *
     * @param data data
     * @param context context
     * @param isVertical isVertical
     */
    public RvListProvider(List<String> data, Context context, boolean isVertical) {
        this.data = data;
        this.context = context;
        this.isVertical = isVertical;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            if (isVertical) {
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_list_vertical, null, false);
            } else {
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_list_horizontal, null, false);
            }
        } else {
            cpt = component;
        }
        Text text = (Text) cpt.findComponentById(ResourceTable.Id_rv_list_btn);
        text.setText(data.get(i));
        return cpt;
    }
}
